package com.rundatop.wx.service.impl;

import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.rundatop.dao.mapper.ProvincesMapper;
import com.rundatop.dao.model.Cities;
import com.rundatop.dao.model.Provinces;
import com.rundatop.dao.model.User;
import com.rundatop.wx.dto.Letter;
import com.rundatop.wx.dto.Page;
import com.rundatop.wx.service.CitiesService;
import com.rundatop.wx.service.ProvincesService;
import com.rundatop.wx.service.StoreService;

import tk.mybatis.mapper.entity.Example;

/**
* 通用 Mapper 代码生成器
*
* @author mapper-generator
*/
@Service
public class ProvincesServiceImpl extends BaseService<Provinces> implements ProvincesService{
	
	@Resource
    private ProvincesMapper provincesMapper;
	@Resource
	private CitiesService citiesService; 
	@Resource
	private StoreService storeService;

    @Override
    public int add(Provinces entity, User loginUser) {

        //if (isBlank(entity.getCode())) {
        //    throw new BusinessException("代码不能为空");
        //}

       return provincesMapper.insertSelective(entity);
    }

    @Override
    public int edit(Provinces entity, User loginUser) {

        //if (isBlank(entity.getId())) {
        //    throw new BusinessException("Id不能为空");
        //}

        return provincesMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public int deleteById(Serializable id, User loginUser) {
       return provincesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Provinces> getList(Provinces condition, Page page, User loginUser) {
        Example ex = new Example(Provinces.class);

        Example.Criteria c = ex.createCriteria();

        //if (isNotBlank(condition.getCode())) {
        //    c.andEqualTo("code", condition.getCode());
        //}

        //if (isNotBlank(condition.getName())) {
        //    c.andLike("name", fullLike(condition.getName()));
        //}

        //ex.orderBy("code").asc();

        if (page != null) {
            PageHelper.startPage(page.getPage(), page.getRows());
        }

        List<Provinces> list = provincesMapper.selectByExample(ex);
        return list;
    }

    @Override
    public Provinces getById(Serializable id) {
        return provincesMapper.selectByPrimaryKey(id);
    }

	@Override
	public List<Letter> getProvincesAndCities() {
		List<Letter> indexesList =new ArrayList<Letter>();
		Letter l = null;
		String letter = "";
		for(int i=(int)'A';i<'A'+26;i++){
			l = new Letter();
			letter = ""+(char)i;
			l.setName(letter);
			l.setLists(getprovincesByLetter(letter));
			indexesList.add(l);
        }
		return indexesList;
	}

	private List<Provinces> getprovincesByLetter(String letter) {
		Example example = new Example(Provinces.class);
		example.createCriteria().andEqualTo("fa", letter);
		List<Provinces> provinceList = provincesMapper.selectByExample(example);
		if (provinceList != null && provinceList.size() > 0) {
			for (Provinces provinces : provinceList) {
				provinces.setList(getCitys(provinces.getProvinceid()));
			}
		}
		return provinceList;
	}

	private List<Cities> getCitys(String id) {
		Example example = new Example(Cities.class);
		example.createCriteria().andEqualTo("provinceid", id);
		List<Cities> cities = citiesService.selectByExample(example);
//		for (Cities city : cities) {
//			city.setList(storeService.getStroe(city.getCityid()));
//		}
		return cities;
	}
}




